using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using CPlatePrintSys.Domain.DTOs;
using Newtonsoft.Json;

namespace CPlatePrintSys.Application.Services
{
    /// <summary>
    /// KMACS系统加工信息获取服务
    /// </summary>
    public class KmacsService
    {
        private bool _useLocalTestData = true;

        // 预留API地址字段，后续可通过配置或注入方式赋值
        private string _apiBaseUrl;
        public string ApiBaseUrl
        {
            get => _apiBaseUrl;
            set => _apiBaseUrl = value;
        }

        public KmacsService(string apiBaseUrl = null)
        {
            _apiBaseUrl = apiBaseUrl;
        }

        /// <summary>
        /// 获取KMACS加工信息（实际应为Restful API调用，当前为mock数据）
        /// </summary>
        /// <returns></returns>
    public async Task<List<KmacsProcessInfoDto>> GetProcessInfosAsync()
        {
            await Task.Delay(50); // 模拟异步
            var list = new List<KmacsProcessInfoDto>();

            //// 原有的1对1数据（S001-S004）
            //for (int i = 1; i <= 4; i++)
            //{
            //    list.Add(new KmacsProcessInfoDto
            //    {
            //        StreamShipNo = $"S{i:D3}",
            //        LotNo = $"L{i:D3}",
            //        Sequence = $"SEQ{i:D3}",
            //        SheetConsistentNo = $"CST{i:D3}",
            //        CNO = $"CUT{i:D3}",
            //        MergedInfo = i % 2 == 0 ? "合并A" : "合并B",
            //        SheetSpecCls = i % 3 == 0 ? "Q235B" : "Q345C",
            //        SheetSpecThickness = 10 + i % 6,
            //        SheetSpecLength = 20000,
            //        SheetSpecWidth = 2000,
            //        PlanUsedDate = DateTime.Now.AddDays(i)
            //    });
            //}

            //// 新增一对多测试数据
            //// S005 船号对应多个LOT号
            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S005",
            //    LotNo = "L005A",
            //    Sequence = "SEQ008",
            //    SheetConsistentNo = "CST008",
            //    CNO = "CUT008",
            //    MergedInfo = "合并A",
            //    SheetSpecCls = "Q235B",
            //    SheetSpecThickness = 16,
            //    SheetSpecLength = 22000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(90)
            //});

            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S005",
            //    LotNo = "L005B",
            //    Sequence = "SEQ009",
            //    SheetConsistentNo = "CST009",
            //    CNO = "CUT009",
            //    MergedInfo = "合并B",
            //    SheetSpecCls = "Q345C",
            //    SheetSpecThickness = 16,
            //    SheetSpecLength = 22000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(90)
            //});

            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S005",
            //    LotNo = "L005C",
            //    Sequence = "SEQ010",
            //    SheetConsistentNo = "CST010",
            //    CNO = "CUT010",
            //    MergedInfo = "合并A",
            //    SheetSpecCls = "Q235B",
            //    SheetSpecThickness = 16,
            //    SheetSpecLength = 22000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(90)
            //});

            //// S006 船号对应多个LOT号，且LOT号对应多个加工序列
            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S006",
            //    LotNo = "L006A",
            //    Sequence = "SEQ011",
            //    SheetConsistentNo = "CST011",
            //    CNO = "CUT011",
            //    MergedInfo = "合并B",
            //    SheetSpecCls = "Q345C",
            //    SheetSpecThickness = 20,
            //    SheetSpecLength = 28000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(105)
            //});

            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S006",
            //    LotNo = "L006A",
            //    Sequence = "SEQ012",
            //    SheetConsistentNo = "CST012",
            //    CNO = "CUT012",
            //    MergedInfo = "合并A",
            //    SheetSpecCls = "Q235B",
            //    SheetSpecThickness = 20,
            //    SheetSpecLength = 28000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(105)
            //});

            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S006",
            //    LotNo = "L006B",
            //    Sequence = "SEQ013",
            //    SheetConsistentNo = "CST013",
            //    CNO = "CUT013",
            //    MergedInfo = "合并B",
            //    SheetSpecCls = "Q345C",
            //    SheetSpecThickness = 20,
            //    SheetSpecLength = 28000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(105)
            //});

            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S006",
            //    LotNo = "L006B",
            //    Sequence = "SEQ014",
            //    SheetConsistentNo = "CST014",
            //    CNO = "CUT014",
            //    MergedInfo = "合并A",
            //    SheetSpecCls = "Q235B",
            //    SheetSpecThickness = 20,
            //    SheetSpecLength = 28000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(105)
            //});

            //list.Add(new KmacsProcessInfoDto
            //{
            //    StreamShipNo = "S006",
            //    LotNo = "L006B",
            //    Sequence = "SEQ015",
            //    SheetConsistentNo = "CST015",
            //    CNO = "CUT015",
            //    MergedInfo = "合并B",
            //    SheetSpecCls = "Q345C",
            //    SheetSpecThickness = 20,
            //    SheetSpecLength = 28000,
            //    SheetSpecWidth = 4000,
            //    PlanUsedDate = DateTime.Now.AddDays(105)
            //});


            if (_useLocalTestData)
            {
                return LoadLocalMockData();
            }

            return list;
        }

        /// <summary>
        /// 从MockData文件夹加载本地数据
        /// </summary>
        public List<KmacsProcessInfoDto> LoadLocalMockData()
        {
            try
            {
                // 获取应用程序所在目录
                string appDirectory = AppDomain.CurrentDomain.BaseDirectory;

                // 构建MockData\KmacsData.json的完整路径
                string mockDataPath = Path.Combine(appDirectory, "MockData", "KmacsData.json");

                // 检查文件是否存在
                if (!File.Exists(mockDataPath))
                {
                    throw new FileNotFoundException($"Mock数据文件未找到: {mockDataPath}");
                }

                // 读取JSON文件内容
                string jsonContent = File.ReadAllText(mockDataPath);

                // 反序列化为DxfDrawingInfoDto列表
                var drawingInfos = JsonConvert.DeserializeObject<List<KmacsProcessInfoDto>>(jsonContent);

                return drawingInfos;
            }
            catch (Exception ex)
            {
                // 记录错误日志（这里可以根据需要添加日志记录）
                throw new InvalidOperationException($"加载本地Mock数据失败: {ex.Message}", ex);
            }
        }
    }
} 